#!/opt/local/bin/perl -w

use strict;
use Time::HiRes qw( usleep );
use String::Escape qw( printable unprintable );
use Net::OpenSoundControl::Client;
use Sys::Syslog;
use threads;
#use threads::shared;
use Data::Dumper;

use constant DIAS_NUM => '20';
use constant DELAY => '30';
use constant BACK_TO_FIRST_DELAY => '120';
use constant OSC_PORT => '1234';
#use constant OSC_HOST => '10.0.1.230';
use constant OSC_HOST => '10.8.0.24';

#my $bpm :shared = 120;

openlog($0, "ndelay,pid", "local0");

my $client = Net::OpenSoundControl::Client->new(Host => OSC_HOST, Port => OSC_PORT)
	or die "Could not start client: $@\n";

my $thr_osc_sender = threads->create('osc_sender');

#$server->readloop();

while (1) {
	sleep 1;
}

sub osc_sender {
	syslog('info', "osc client started, sending on port OSC_PORT");
	print Dumper "started...";
	$client->send(['/sync', 'f', 1]);
	usleep(DELAY * 1000_000);
	while (1) {
#		$client->send(['/lamp', 'f', 1]);
#		usleep(DELAY * 1000_000);
#		$client->send(['/lamp', 'f', 0]);

		for (1..(DIAS_NUM - 1)) {
			$client->send(['/forward', 'f', 1]);
			syslog('info', "forward");
			usleep(DELAY * 1000_000);
		}
		$client->send(['/forward', 'f', 1]);
		syslog('info', "to #1");
		usleep(BACK_TO_FIRST_DELAY * 1000_000);
	}
}

1;
